//
//  CpGReporter.m
//  iBioCAD
//
//  Created by Orbitus on 10/24/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "CpGReporter.h"


@implementation CpGReporter

- (void) awakeFromNib
{
	[self init];
}

- (id)init {
    self = [super init];
	[[NSNotificationCenter defaultCenter] addObserver:self 
											selector:@selector(finishedMuscle:) 
												name:NSTaskDidTerminateNotification 
												object:nil];
	myCpG = nil; // This is a good time to initialize the pointer
	return self;
}

- (IBAction)CpG:(id)sender
{
	[cpgButton setTitle:@"Running..."];
	NSLog(@"Running CpG ...");
    [cpgButton setEnabled:NO];
	
	[appRunningIndicator startAnimation:self];
	
	myCpG = [[NSTask alloc] init];
	newCpGSeek = [[NSTask alloc] init];
	cpgPlot = [[NSTask alloc] init];
	newCpGreport = [[NSTask alloc] init];
	
	pipe = [[NSPipe alloc] init];
	id pipe2 = [[NSPipe alloc] init];
	id pipe3 = [[NSPipe alloc] init];
	id pipe4 = [[NSPipe alloc] init];

	NSFileHandle *handle;
	
	myDesktopFolderPath = @"/Sequences/iBioCAD/Desktop";
	NSString *mySequenceFolderPath	= @"/Sequences/iBioCAD/Desktop/sequence.fasta";
	cpgReportPath = @"/UnixApps/EMBOSS/emboss/cpgreport";
	newCpGSeekPath = @"/UnixApps/EMBOSS/emboss/newcpgseek";
	newCpGReportPath = @"/UnixApps/EMBOSS/emboss/newcpgreport";
	cpgPlotPath = @"/UnixApps/EMBOSS/emboss/cpgplot";
	 
	[myCpG setLaunchPath:cpgReportPath];
	[newCpGSeek setLaunchPath:newCpGSeekPath];
	[cpgPlot setLaunchPath:cpgPlotPath];
	[newCpGreport setLaunchPath:newCpGReportPath];
	
	[myCpG setCurrentDirectoryPath:myDesktopFolderPath];
	[newCpGSeek setCurrentDirectoryPath:myDesktopFolderPath];
	[cpgPlot setCurrentDirectoryPath:myDesktopFolderPath];
	[newCpGreport setCurrentDirectoryPath:myDesktopFolderPath];
	
	[myCpG setArguments:[NSArray arrayWithObjects:@"-sequence", mySequenceFolderPath,@"-score",@"17",@"-outfile", @"/Sequences/iBioCAD/Desktop/sequence.cpgreport", @"-outfeat", @"/Sequences/iBioCAD/Desktop/sequence.cpgreport.gff",nil]];
	[newCpGSeek setArguments:[NSArray arrayWithObjects:@"-sequence", mySequenceFolderPath,@"-score", @"17",@"-outfile", @"/Sequences/iBioCAD/Desktop/sequence.newcpgseek",nil]];
	[cpgPlot setArguments:[NSArray arrayWithObjects:@"-sequence", mySequenceFolderPath,@"-window",@"100",@"-minlen",@"200",@"-minoe",@"0.6",@"-minpc",@"50",@"-outfile", @"/Sequences/iBioCAD/Desktop/sequence.cpgplot",@"-graph",@"data", @"-outfeat", @"/Sequences/iBioCAD/Desktop/sequence.cpgplot.gff",nil]];
	[newCpGreport setArguments:[NSArray arrayWithObjects:@"-sequence", mySequenceFolderPath,@"-window",@"100",@"-shift",@"1",@"-minlen",@"200",@"-minoe",@"0.6",@"-minpc",@"50",@"-outfile", @"/Sequences/iBioCAD/Desktop/sequence.newcpgreport",nil]];

	[myCpG setStandardOutput:pipe];
	[newCpGSeek setStandardOutput:pipe2];
	[cpgPlot setStandardOutput:pipe3];
	[newCpGreport setStandardOutput:pipe4];
	
	//	handle=[NSFileHandle fileHandleForReadingAtPath:[myBinFolderPath stringByAppendingPathComponent: @"out.fasta"]];
	
	handle=[pipe fileHandleForReading];

	[myCpG launch];
	[newCpGSeek launch];	
	[cpgPlot launch];
	[newCpGreport launch];
	
 	//[NSThread detachNewThreadSelector:@selector(copyData:)
	//						toTarget:self withObject:handle];
	
	//string=[[NSString alloc] initWithData:[handle readDataToEndOfFile] encoding:NSASCIIStringEncoding]; // convert NSData -> NSString
    
    [cpgOutput setString:@"Finished CpG Alignment"];
	NSLog(@"...Finished CpG!");
	
}
- (void)copyData:(NSFileHandle*)handle {
    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
    NSData *data;
	
    while([data=[handle availableData] length]) { // until EOF (check reference)
        NSString *string=[[NSString alloc] initWithData:data
																															 encoding:NSASCIIStringEncoding];
        NSRange theEnd=NSMakeRange([[cpgOutput string] length],0);
		
        [cpgOutput replaceCharactersInRange:theEnd
																							 withString:string]; // append new string to the end
        theEnd.location+=[string length]; // the end has moved
        [cpgOutput scrollRangeToVisible:theEnd];
		
        [string release];
    }
	
    [pool release];
}

- (void)finishedCpG:(NSNotification *)aNotification
{
	//[cpgButton setTitle:@"CpGReporter"];
	//[cpgButton setEnabled:YES];
	//[appRunningIndicator stopAnimation:self];
	
	//[myBlast release]; // Don't forget to clean up memory
    //[pipe release];
}

@end